﻿using GHI.Glide;
using Microsoft.SPOT;
using PJE.GlideEx.UI;
using Timer = Gadgeteer.Timer;
using Window = GHI.Glide.Display.Window;

namespace AnimatedImageTest
{
	public partial class Program
	{
		private Window _window;
		private AnimatedImage _aniimg1;
		private AnimatedImage _aniimg2;
		private AnimatedImage _aniimg3;
		private AnimatedImage _aniimg4;
		private AnimatedImage _aniimg5;
		private AnimatedImage _aniimg6;

		private AnimatedImage _aniimg7;
		private AnimatedImage _aniimg8;
		private AnimatedImage _aniimg9;
		private AnimatedImage _aniimg10;
		private AnimatedImage _aniimg11;
		private AnimatedImage _aniimg12;

		private AnimatedImage _aniimg13;
		private AnimatedImage _aniimg14;
		private AnimatedImage _aniimg15;
		private AnimatedImage _aniimg16;
		private AnimatedImage _aniimg17;
		private AnimatedImage _aniimg18;

		private Timer _timer;

		private void ProgramStarted()
		{
			Debug.Print("Program Started");

			_timer = new Timer(25);
			_timer.Tick += _timer_Tick;
			_window = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.window));

			var bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence1), Bitmap.BitmapImageType.Jpeg);

			_aniimg1 = new AnimatedImage("aniimg1", 255, 30, 31, 64, 64);
			_aniimg2 = new AnimatedImage("aniimg2", 255, 100, 31, 64, 64);
			_aniimg3 = new AnimatedImage("aniimg3", 255, 170, 31, 64, 64);
			_aniimg4 = new AnimatedImage("aniimg1", 255, 240, 31, 64, 64);
			_aniimg5 = new AnimatedImage("aniimg2", 255, 310, 31, 64, 64);
			_aniimg6 = new AnimatedImage("aniimg3", 255, 380, 31, 64, 64);

			_aniimg7 = new AnimatedImage("aniimg1", 255, 30, 101, 64, 64);
			_aniimg8 = new AnimatedImage("aniimg2", 255, 100, 101, 64, 64);
			_aniimg9 = new AnimatedImage("aniimg3", 255, 170, 101, 64, 64);
			_aniimg10 = new AnimatedImage("aniimg1", 255, 240, 101, 64, 64);
			_aniimg11 = new AnimatedImage("aniimg2", 255, 310, 101, 64, 64);
			_aniimg12 = new AnimatedImage("aniimg3", 255, 380, 101, 64, 64);

			_aniimg13 = new AnimatedImage("aniimg1", 255, 30, 171, 64, 64);
			_aniimg14 = new AnimatedImage("aniimg2", 255, 100, 171, 64, 64);
			_aniimg15 = new AnimatedImage("aniimg3", 255, 170, 171, 64, 64);
			_aniimg16 = new AnimatedImage("aniimg1", 255, 240, 171, 64, 64);
			_aniimg17 = new AnimatedImage("aniimg2", 255, 310, 171, 64, 64);
			_aniimg18 = new AnimatedImage("aniimg3", 255, 380, 171, 64, 64);

			_aniimg1.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence2), Bitmap.BitmapImageType.Jpeg);
			_aniimg2.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence3), Bitmap.BitmapImageType.Jpeg);
			_aniimg3.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence4), Bitmap.BitmapImageType.Jpeg);
			_aniimg4.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence5), Bitmap.BitmapImageType.Jpeg);
			_aniimg5.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence6), Bitmap.BitmapImageType.Jpeg);
			_aniimg6.Bitmap = bitmap;

			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence7), Bitmap.BitmapImageType.Jpeg);
			_aniimg7.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence8), Bitmap.BitmapImageType.Jpeg);
			_aniimg8.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence9), Bitmap.BitmapImageType.Jpeg);
			_aniimg9.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence10), Bitmap.BitmapImageType.Jpeg);
			_aniimg10.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence11), Bitmap.BitmapImageType.Jpeg);
			_aniimg11.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence12), Bitmap.BitmapImageType.Jpeg);
			_aniimg12.Bitmap = bitmap;

			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence13), Bitmap.BitmapImageType.Jpeg);
			_aniimg13.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence14), Bitmap.BitmapImageType.Jpeg);
			_aniimg14.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence15), Bitmap.BitmapImageType.Jpeg);
			_aniimg15.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence16), Bitmap.BitmapImageType.Jpeg);
			_aniimg16.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence17), Bitmap.BitmapImageType.Jpeg);
			_aniimg17.Bitmap = bitmap;
			bitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.ImageSequence18), Bitmap.BitmapImageType.Jpeg);
			_aniimg18.Bitmap = bitmap;

			_aniimg1.TapEvent += _aniimg1_TapEvent;

			_window.AddChild(_aniimg1);
			_window.AddChild(_aniimg2);
			_window.AddChild(_aniimg3);
			_window.AddChild(_aniimg4);
			_window.AddChild(_aniimg5);
			_window.AddChild(_aniimg6);

			_window.AddChild(_aniimg7);
			_window.AddChild(_aniimg8);
			_window.AddChild(_aniimg9);
			_window.AddChild(_aniimg10);
			_window.AddChild(_aniimg11);
			_window.AddChild(_aniimg12);

			_window.AddChild(_aniimg13);
			_window.AddChild(_aniimg14);
			_window.AddChild(_aniimg15);
			_window.AddChild(_aniimg16);
			_window.AddChild(_aniimg17);
			_window.AddChild(_aniimg18);

			Glide.MainWindow = _window;
			_timer.Start();
		}

		void _aniimg1_TapEvent(object sender)
		{
			Debug.Print("Tab");
			//throw new System.NotImplementedException();
		}

		void _timer_Tick(Timer timer)
		{
			_aniimg1.NextFrame();
			_aniimg2.NextFrame();
			_aniimg3.NextFrame();
			_aniimg4.NextFrame();
			_aniimg5.NextFrame();
			_aniimg6.NextFrame();

			_aniimg7.NextFrame();
			_aniimg8.NextFrame();
			_aniimg9.NextFrame();
			_aniimg10.NextFrame();
			_aniimg11.NextFrame();
			_aniimg12.NextFrame();

			_aniimg13.NextFrame();
			_aniimg14.NextFrame();
			_aniimg15.NextFrame();
			_aniimg16.NextFrame();
			_aniimg17.NextFrame();
			_aniimg18.NextFrame();

			_aniimg1.Invalidate();
			_aniimg2.Invalidate();
			_aniimg3.Invalidate();
			_aniimg4.Invalidate();
			_aniimg5.Invalidate();
			_aniimg6.Invalidate();

			_aniimg7.Invalidate();
			_aniimg8.Invalidate();
			_aniimg9.Invalidate();
			_aniimg10.Invalidate();
			_aniimg11.Invalidate();
			_aniimg12.Invalidate();

			_aniimg13.Invalidate();
			_aniimg14.Invalidate();
			_aniimg15.Invalidate();
			_aniimg16.Invalidate();
			_aniimg17.Invalidate();
			_aniimg18.Invalidate();
		}
	}
}
